package com.tttshaoqi.server.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttshaoqi.common.result.PageResult;
import com.tttshaoqi.common.result.Result;
import com.tttshaoqi.pojo.entity.Flight;
import com.tttshaoqi.pojo.entity.Order;
import com.tttshaoqi.pojo.entity.User;
import com.tttshaoqi.pojo.vo.OrderVO;
import com.tttshaoqi.server.mapper.FlightMapper;
import com.tttshaoqi.server.mapper.OrderMapper;
import com.tttshaoqi.server.mapper.UserMapper;
import com.tttshaoqi.server.service.FlightService;
import com.tttshaoqi.server.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author tttshaoqi
 * &#064;create 2025-04-23-17:14
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FlightMapper flightMapper;

    /**
     * 分页查询
     *
     * @param beginTime
     * @param endTime
     * @param status
     * @param number
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult page(LocalDateTime beginTime, LocalDateTime endTime, Integer status, Integer number, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<OrderVO> list = orderMapper.list(beginTime,endTime,status,number,null);
        Page<OrderVO> pageResult = (Page<OrderVO>) list;
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }

    /**
     * 修改订单状态
     *
     * @param status
     * @param id
     */
    @Override
    public void update(Integer status, Integer id) {
        if(status == 3){
            Integer flightId = orderMapper.getFId(id);
            Flight flight = flightMapper.getById(flightId);
            flight.setSeatAvailable(flight.getSeatAvailable() + 1);
            flightMapper.update(flight);
        }
        orderMapper.update(status,id);
    }

    /**
     * 新增订单信息
     * @param id
     * @param userName
     * @param userPhone
     * @param userID
     * @return
     */
    @Override
    public Result add(Integer id, BigDecimal price, String userName, String userPhone, String userID) {
        User user = userMapper.getByUserID(userID);
        if(user == null){
            user = new User();
            user.setName(userName);
            user.setNumber(userID);
            user.setPhone(userPhone);
            userMapper.add(user);
        }
        Flight flight = flightMapper.getById(id);
        if(flight==null){
            return Result.error("飞机票信息不存在");
        }
        if(flight.getSeatAvailable()==0){
            return Result.error("没坐！");
        }
        flight.setSeatAvailable(flight.getSeatAvailable()-1);
        flightMapper.update(flight);

        Order order = new Order();
        order.setCreateTime(LocalDateTime.now());
        order.setStatus(1);
        order.setFlightId(id);
        order.setPrice(price);
        order.setCustomerId(user.getId());
        orderMapper.add(order);
        return Result.success();
    }
}
